// app.js
import { staff_login, volunteer_login } from "./utils/login_register"
import { startTokenRefresh } from "./utils/util"
import { getRole } from "./utils/match_role"
App({
  onLaunch() {
    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)

    // 检查用户登录状态
    const userInfo = wx.getStorageSync('userInfo')
    // console.log('userid', userid)

    if (userInfo) {
      // 已登录
      //工作人员登录
      if (userInfo.role === 'Staff' || userInfo.role === 'Manager') {
        console.log('工作人员登录')
        //重新登陆
        // 检查是否有保存的登录信息
        const savedAccount = wx.getStorageSync('savedAccount')
        const savedPassword = wx.getStorageSync('savedPassword')
        if (savedAccount && savedPassword) {
          staff_login(savedAccount, savedPassword).then(res => {
            if (res.code === 200) {
              // console.log(res)
              const role = getRole(res.loginuser.role)
              // 保存用户信息
              let userInfo = {
                userid: res.loginuser.uid,
                username: res.loginuser.userName,
                role: role,
                name: res.loginuser.name,
                token: res.token
              }
              wx.setStorage({
                key: 'userInfo',
                data: userInfo
              })

              // 更新全局数据
              const app = getApp()
              app.globalData.userInfo = userInfo
              app.globalData.isLoggedIn = true
              //延时1秒
              setTimeout(() => {
                // 开始定时刷新token
                startTokenRefresh()
              }, 1000)

              // 跳转到首页
              wx.switchTab({
                url: '/pages/index/index'
              })
            }
          })
        } else {
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }
      }
      //志愿者登录
      else if (userInfo.role === 'Volunteer') {
        console.log('志愿者登录')
        //重新登录
        // 检查是否有保存的登录信息
        const savedAccount = wx.getStorageSync('savedAccount')
        const savedPassword = wx.getStorageSync('savedPassword')
        if (savedAccount && savedPassword) {
          volunteer_login(savedAccount, savedPassword).then(res => {
            if (res.code === 200) {
              const role = getRole(res.loginuser.role)
              // 保存用户信息
              let userInfo = {
                userid: res.loginuser.uid,
                username: res.loginuser.userName,
                role: role,
                token: res.token
              }
              wx.setStorage({
                key: 'userInfo',
                data: userInfo
              })

              // 更新全局数据
              const app = getApp()
              app.globalData.userInfo = userInfo
              app.globalData.isLoggedIn = true

              //延时1秒
              setTimeout(() => {
                // 开始定时刷新token
                startTokenRefresh()
              }, 1000)

              // 跳转到首页 
              wx.redirectTo({
                url: '/pages/index/volunteerindex/volunteerindex'
              })
            }
          })
        } else {
          wx.redirectTo({
            url: '/pages/login/volunteerlogin/volunteerlogin'
          })
        }
      }
    } else {
      // 未登录，跳转到登录页
      console.log('未登录')
      this.globalData.isLoggedIn = false
      wx.redirectTo({
        url: '/pages/login/login'
      })
    }
  },
  globalData: {
    userInfo: null,
    isLoggedIn: false,
  }
})